<template>
	<el-container>
		<!-- <el-aside width="200px">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="id" :data="groupData" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
			</el-container>
		</el-aside> -->
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
					<div class="choose">
						<el-select v-model="floor_id" placeholder="请选择楼层" @change="handleFloorChange">
							<el-option v-for="option in floorinfo" :key="option.id" :label="option.name" :value="option.id"  />
						</el-select>	
						<el-select v-model="room_id" placeholder="请选择房间" @change="handleRoomChange">
							<el-option v-for="option in roominfo" :key="option.id" :label="option.name" :value="option.id"  />
						</el-select>	
						<el-select v-model="type" placeholder="请选择类型" @change="handleTypeChange">
							<el-option v-for="option in typelistinfo" :key="option.type" :label="option.type_text" :value="option.type"  />
						</el-select>
						<el-button class="reset" round type="danger" size="small" @click="reload">清空选项</el-button>	
					</div>
				</div>
				
				<div class="right-panel">
					<!-- <div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="关键词" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div> -->
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="list.apiObj" row-key="id" @selection-change="selectionChange" stripe>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="Id" prop="id" width="180"></el-table-column>
					<el-table-column label="楼层名字" prop="floor.name" width="180"></el-table-column>
					<el-table-column label="房间名字" prop="room.name" width="180"></el-table-column>
					<el-table-column label="设备名字" prop="name" width="180"></el-table-column>
					<el-table-column label="设备账号" prop="account" width="180"></el-table-column>
					<el-table-column label="设备类型" prop="type_text" width="180"></el-table-column>
					<el-table-column label="状态" prop="status" width="60">
						<template #default="scope">
							<sc-status-indicator v-if="scope.row.status" type="success"></sc-status-indicator>
							<sc-status-indicator v-if="!scope.row.status" pulse type="danger"></sc-status-indicator>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="center" width="160">
						<template #default="scope">
							<router-link v-if="scope.row.type == 2" :to="{name: 'branch', params: { device_id: scope.row.id }}">
									<el-button text type="primary" size="small">管理</el-button>
							</router-link>
							<el-button-group>	
								<el-button text type="primary" size="small" @click="row_show(scope.row)">查看</el-button>
								<el-button text type="primary" size="small" @click="row_edit(scope.row)">编辑</el-button>
								<el-button text type="primary" size="small" @click="row_del(scope.row, scope.$index)">删除</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveD" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
	import saveDialog from "./save.vue";
	
	export default {
		components: {
			saveDialog,
		},
		name: 'listTree',
		data() {
			return {
				floor_id: null,
				room_id: null,
				type: null,
				floorinfo: '',
				typelistinfo: '',
				roominfo: '',
				dialog:{
					save: false,
					//info: false,
				},
				selection: [],
				//groupFilterText: "",
				// groupData: [
				// 	{
				// 		id: "",
				// 		label: "所有"
				// 	},
				// 	{
				// 		id: "1",
				// 		label: "华东总部",
				// 		children: [
				// 			{
				// 				id: "11",
				// 				label: "售前客服部"
				// 			},
				// 			{
				// 				id: "12",
				// 				label: "技术研发部"
				// 			},
				// 			{
				// 				id: "13",
				// 				label: "行政人事部"
				// 			}
				// 		]
				// 	},
				// 	{
				// 		id: "2",
				// 		label: "华难总部",
				// 		children: [
				// 			{
				// 				id: "21",
				// 				label: "售前客服部"
				// 			},
				// 			{
				// 				id: "22",
				// 				label: "技术研发部"
				// 			}
				// 		]
				// 	}
				// ],
				list: {
					apiObj: this.$API.device.iotlist
				},
				search: {
					keyword: ""
				}
			}
		},
		mounted() {
			this.getFloorinfo() // 获取楼层信息选项
			this.getRoominfo(0) // 获取房间信息选项  0 代表无特定楼层的所有房间
			this.getType() // 获取type列表，此type不包含网关(type = 1)
		},
		watch: {
			// groupFilterText(val) {
			// 	this.$refs.group.filter(val);
			// }
		},
		methods: {
			handleFloorChange (val) {
            	this.room_id = null; // 清除房间选择框
				this.getRoominfo(val)
				var params = {
					floor_id: val,
					room_id: null
				}
				this.$refs.table.upData(params)
				// if (this.roominfo.length > 0) {  
     			// 	this.room_id = this.roominfo[0].id; // 如果数组长度大于0， 则默认选中第一个元素
    			// }
			},
			handleRoomChange (val) {
				var params = {
					room_id: val
				}
				this.$refs.table.upData(params)
				// if (val !== null) {
				// 	let data = this.roominfo.find(item => item.id === val)
				// 	this.floor_id = data.f_id;
				// }
			},
			handleTypeChange (val) {
				var params = {
					type: val
				}
				this.$refs.table.upData(params)
			},
			async getType()
			{
				var res = await this.$API.device.typelist.get();
				if(res.code == 200){
					this.typelistinfo = res.data
				}
			},
			async getFloorinfo()
			{
				var res = await this.$API.floor.list.get();
				if(res.code == 200){
					this.floorinfo = res.data.rows
				}
			},
			async getRoominfo(value)
			{
				console.log(value)
				let param;
				if (value) {
					param = {
						'pageSize' : 100, 
						'f_id': value
					};
				} else {
					param = {
						'pageSize' : 1000, // 单页选中1000个房间，确保房间被选中
					};
				}
				var res = await this.$API.floor.roomList.get(param);
				if(res.code == 200){
					this.roominfo = res.data.rows
				}
			},
			reload() {
				this.type = null
				this.floor_id =null
				this.room_id = null
				this.$refs.table.reload()
			},		
			//树过滤
			// groupFilterNode(value, data){
			// 	if (!value) return true;
			// 	return data.label.indexOf(value) !== -1;
			// },
			//树点击事件
			// groupClick(data){
			// 	var params = {
			// 		groupId: data.id
			// 	}
			// 	this.$refs.table.reload(params)
			// },
			//搜索
			// upsearch(){
			// 	this.$refs.table.upData(this.search)
			// },
			add() {
				this.dialog.save=true
				this.$nextTick(() => {
					this.$refs.saveD.open('add')
				})
			},
			row_show(val) { //  查看功能
				this.dialog.save=true
				this.$nextTick(() => {
					this.$refs.saveD.open('show').setData(val);
				})
			},
			row_edit(val) { //  编辑功能
				this.dialog.save=true
				this.$nextTick(() => {
					this.$refs.saveD.open('edit').setData(val);
				})
			},
			async row_del(val, index) { //  删除功能
				console.log(index)
				var reqData = val.id
				var res = await this.$API.device.delete.delete(reqData);
				if(res.code == 200){
					//this.$refs.table.removeIndex(index)
					this.$message.success("删除成功")
					this.$refs.table.refresh()
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection
			},
			//批量删除
			async batch_del(){
				var confirmRes = await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '删除',
					confirmButtonClass: 'el-button--danger'
				}).catch(() => {})
				if(!confirmRes){
					return false
				}	
				var ids = this.selection.map(v => v.id)
				this.$refs.table.removeKeys(ids)
				var res = await this.$API.device.delete.delete(ids);
				if(res.code == 200){
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				console.log(data,mode)
				//为了减少网络请求，直接变更表格内存数据
				// if(mode=='add'){
				// 	this.$refs.table.unshiftRow(data)
				// }else if(mode=='edit'){
				// 	console.log(data)
				// 	this.$refs.table.updateKey(data)
				// }
				//当然也可以暴力的直接刷新表格
				this.$refs.table.refresh()
			}
		}
	}
</script>

<style scoped>
.choose {
	margin-left: 20px
}
.reset {
	margin-left: 10px
}
</style>
